<%@ page import="com.scalar.chiptrack.sales.forms.TopCustomersForm,
                 com.scalar.chiptrack.utils.ConfigManager,
                 java.util.ArrayList,
                 com.scalar.chiptrack.sales.TopCustomers,
                 com.scalar.chiptrack.commons.Constants,
                 com.scalar.chiptrack.commons.SessionKeys" %>
<%@ page import="com.scalar.chiptrack.users.Functionality" %>
<%@ page import="com.scalar.chiptrack.sales.TopCustomers" %>
<%@ page import="com.scalar.chiptrack.sales.TopCustomer" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld"    prefix="bean"      %>
<%@ taglib uri="/WEB-INF/struts-html.tld"    prefix="html"      %>
<%@ taglib uri="/WEB-INF/struts-logic.tld"   prefix="logic"     %>
<%@ taglib uri="/WEB-INF/struts-nested.tld"  prefix="nested"    %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%try{%>

<chiptrack:checkLogon sourcePage="/topCustomers.do" />

<% String chipTracContext = "/"+ConfigManager.getChipTracContext(); %>
<% User user1 = (User) session.getAttribute (SessionKeys.USER_KEY); %>
<HTML>
<HEAD>
<TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script language="javascript">
var defaultStr = "";
function sendOptionElementLeftToRight ( index )
{
    var list1 = document.topCustomersForm[ 'topCustomer.topCustomers[' + index +'].availablecustomers'];
    var list2 = document.topCustomersForm[ 'topCustomer.topCustomers[' + index +'].selectedCustomers'];
    var noOfBoxesObj   = document.topCustomersForm['topCustomer.totalRanks'];
    if ( list_copySelected ( list1,list2, defaultStr ) > 0 )
    {
        for ( var i= 0; i <= 19; i++ )
        {
            if( index != i ) {
                var list = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].availablecustomers'];
                list_deleteAll( list1, list, defaultStr );
            }
        }
        list_deleteSelected ( list1, defaultStr );
    }
}
function sendOptionElementRightToLeft ( index )
{
    var list1 = document.topCustomersForm[ 'topCustomer.topCustomers[' + index +'].availablecustomers'];
    var list2 = document.topCustomersForm[ 'topCustomer.topCustomers[' + index +'].selectedCustomers'];
    var noOfBoxesObj   = document.topCustomersForm['topCustomer.totalRanks'];

    for ( var i= 0; i <= 19; i++ )
    {
        if( index != i ) {
            var list = document.topCustomersForm[ 'topCustomer.topCustomers[' + i+'].availablecustomers'];
            list_copySelected( list2, list, defaultStr )
        }
    }
    if ( list_copySelected ( list2, list1, defaultStr ) > 0 )
    {
        list_deleteSelected ( list2, defaultStr );
    }
}

function list_copySelected(list1, list2, defaultStr)
{

    if ( list1 == null || list2 == null ){
        return -1;
    }
    else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
        return -2;
    }
    var copyCount = 0;

    // unselect all in list2 first (IE5 bug)
    for (var i = 0; i < list2.length; i++ ){
            list2.options[i].selected = false;
    }

    // copy the selected items from list1 to list2
    for (var i = 0; i < list1.length; i++ ){
        if ( list1.options[i].selected ){
            list2.options[list2.length] = new Option(list1.options[i].text, list1.options[i].value);
            copyCount++;
        }
    }

    // remove the default str from list2 if neccesary
    if ( list2.length > 1 && list2.options[0].text == defaultStr ){
        list2.options[0] = null;
    }

    return copyCount;
}
function list_deleteSelected(list1, defaultStr)
{
    // assertions
    if ( list1 == null ){
        return -1;
    }
    else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
        return -2;
    }

    // copy the selected items from list1 to the array
    // use trick with array because of IE5 bug
    var deleteCount = 0;
    var sel = new Array(list1.length);

    for (var i = 0; i < list1.length; i++ ){
        if ( list1.options[i].selected ){
            list1.options[i].selected = false;
            sel[deleteCount++] = i;
        }
    }

    for (var i = deleteCount - 1; i > -1 ; i-- ){
        list1.options[sel[i]] = null;
    }

    if ( list1.length == 0 )
        list1.options[0] = new Option(defaultStr, defaultStr);

    return deleteCount;
}

function list_deleteAll( list1, list, defaultStr )
{
    // assertions
    if ( list1 == null ){
        return -1;
    }
    else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
        return -2;
    }

    // copy the selected items from list1 to the array
    // use trick with array because of IE5 bug
    var deleteCount = 0;
    var sel = new Array(list1.length);

    for (var i = 0; i < list1.length; i++ ){
        if ( list1.options[i].selected ){
            sel[deleteCount++] = i;
        }
    }

    for (var i = deleteCount - 1; i > -1 ; i-- ){
        list.options[sel[i]] = null;
    }

    if ( list.length == 0 )
        list.options[0] = new Option(defaultStr, defaultStr);

    return deleteCount;
}

var pa = '';
function selectAll()
{
    var noOfBoxesObj = document.topCustomersForm['topCustomer.totalRanks'];
    if ( typeof  noOfBoxesObj != 'undefined' )
    {
        for( var i = 0 ; i < 19 ; i++ )
        {
            var selectedStageNameObject = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].selectedCustomers'];
            var availableStageNameObject = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].availablecustomers'];

            for ( var j = 0; j < selectedStageNameObject.length; j++ )
            {
                selectedStageNameObject.options[j].selected = true;
            }
            for ( var k = 0; k < availableStageNameObject.length; k++ )
            {
            availableStageNameObject.options[k].selected = true;
            }
        }
     }
}
function deSelectAll()
{
    var noOfBoxesObj = document.topCustomersForm['topCustomer.totalRanks'];

    if ( typeof  noOfBoxesObj != 'undefined' )
    {
        for( var i = 0 ; i < 19 ; i++ )
        {
            var selectedStageNameObject = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].selectedCustomers'];
            var availableStageNameObject = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].availablecustomers'];

            for ( var j = 0; j < selectedStageNameObject.length; j++ )
            {
                selectedStageNameObject.options[j].selected = false;
            }
            for ( var k = 0; k < availableStageNameObject.length; k++ )
            {
                availableStageNameObject.options[k].selected = false;
            }
        }
     }
}
function saveTopCustomers()
{
    pa = document.getElementById("message");
    var frm = document.topCustomersForm['topCustomer'];
    var noOfBoxesObj = 20;
    for( var i = 0 ; i < 20 ; i++ )
    {
        var selectedStageNameObject = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].selectedCustomers'];
        var groupNameTextObj = document.topCustomersForm[ 'topCustomer.topCustomers[' + i +'].customersAlisName'];

        groupNameTextObj.value = trim(groupNameTextObj.value);

        

        for( var j = 0 ; j < selectedStageNameObject.length ; j++ )
        {
            selectedStageNameObject.options[j].selected = true;
        }

        if ( selectedStageNameObject.value == '' )
        {
            pa.innerHTML = "<li>Selected Customers should not be null</li>";
            deSelectAll();
            return ;
        }
        else
        {
            if ( groupNameTextObj.value == '' )
            {
                pa.innerHTML = "<li>Enter The Customer Group Name</li>";
                groupNameTextObj.focus();
                deSelectAll();
                return ;
            }

        }
          for( var j = i+1; j < 20; j++ )
        {
           var nextgroupNameTextObj = document.topCustomersForm[ 'topCustomer.topCustomers[' + j +'].customersAlisName'];
           if ( groupNameTextObj.value == nextgroupNameTextObj.value )
           {
                pa.innerHTML = "<li>Customer Group Names should Not be Same</li>";
                nextgroupNameTextObj.focus();
                deSelectAll();
                return ;
           }
        }

    }

    document.topCustomersForm.action.value="saveTopCustomers";
    document.topCustomersForm.submit();
}
function loadCustomers()
{
    var frm = document.topCustomersForm;
    if(frm.customerType.value!= 0)
    {
        document.topCustomersForm.action.value="loadTopCustomers";
        document.topCustomersForm.submit();
    }
}
function load()
{
    deSelectAll();
}
</script>
</HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" onLoad="load()">
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
	    <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
    <!-- Side Navigation start -->
    <%@ include file="/jsp/sales/sales_menu.jsp" %>
    <!-- end of the side navigations -->
    <TD bgcolor="#ffffff">
        <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8>
    </TD>
    <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
    <TD valign="top" width="100%">
    <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
    <BR>
    <html:form name="topCustomersForm" action="/topCustomers.do" method="post" type="com.scalar.chiptrack.sales.forms.TopCustomersForm" scope="session">
    <% TopCustomers topCustomersobj = new TopCustomers();%>
    <table border="0" width="90%" align=center>
        <tr><td colspan=5 class="NArialW" align="center">Top Customers</td></tr>
        <tr><td colspan=5>&nbsp;</td></tr>
        <tr><td colspan=5 class='NArialL'><strong><FONT id="message" styleClass="NArialL" color='FF0000'></FONT></td></tr>
        <tr><td colspan=5>
                <chiptrack:info styleClass="NArialL" />
                <chiptrack:errors styleClass="NArialL" />
            </td>
        </tr>
        <% if (!user1.isActionAllowed(Functionality.GROUP_FAB_STAGES, User.USER_ACTION_MODIFY)) { %>
             <tr><td colspan=4 align='left'><font class='NArialS'><font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b></font></td></tr> <% } %>
        <tr><td colspan=5>&nbsp;</td></tr>
        <tr><td>
                <table border="0" width="90%">
                    <tr>
                        <td width="23%">&nbsp;</td>
                        <td width="20%" class="NArialL" >Customer Type:</td>
                        <td width="25%" class="NArialL">
                            <nested:select name="topCustomerForm" property="customerType" styleClass="txfield" onchange = "loadCustomers();"> 
                                <html:option value="0">Select Customer Type</html:option>
                                <html:options collection="customerTypes" property="value" labelProperty="label"/>
                            </nested:select>
                        </td>
                        <td width="2%">&nbsp;</td>
                    </tr>
                    <tr><td colspan="5" width="90%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
                </table>
                <logic:equal name="showCustomers" value="true" >
                <nested:nest property = "topCustomer" >
                <TABLE width="95%" border = "0">
                    <TR>
                        <td width ="15%" >&nbsp;</td>
                        <TD class="NArialL" width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Rank </font></td>
                        <TD class="NArialL" width="20%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"> Available Customers </font></TD>
                        <TD width="5%" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">&nbsp;</font></TD>
                        <TD class="NArialL" width="20%"  align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"> Selected Customers  </font></TD>
                        <TD width="15%" bgcolor="#993333" align="center" class="NArialL"><font color="#FFFFFF">Customer Group Name</font></TD>
                        <TD width="20%">&nbsp;</TD>
                    </TR>

                    <nested:iterate id="topCustomers" property="topCustomers" indexId= "stageItemid">

                    <TR >
                        <td width ="15%" >&nbsp;</td>
                        <TD class="NArialL" align="center" width ="10%" <%if(stageItemid.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%>> &nbsp;<nested:write property="rank"/> </TD>
                        <TD width="20%" <%if(stageItemid.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%>>
                            <nested:select  name="topCustomers" property="availablecustomers"  styleClass="emptyList" size="10" multiple = "true" >
                                 <html:options collection="availableCustomersList" property="value" labelProperty="label"/>
                            </nested:select>
                        </TD>
                        <TD width="5%" align="center" <%if(stageItemid.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%>>
                            <%
                                String jsFunctionStrLeft = "sendOptionElementLeftToRight(" + stageItemid + ");";
                                String jsFunctionStrRight = "sendOptionElementRightToLeft(" + stageItemid + ");";
                            %>
                            <input type="button" value=">>" onclick="<%=jsFunctionStrLeft%>">
                            <br>
                            <input type="button" value="<<"  onclick="<%=jsFunctionStrRight%>">
                        </TD>
                        <TD width="20%" <%if(stageItemid.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%>>
                            <nested:select name="topCustomers" property="selectedCustomers" styleClass="emptyList" size="10" multiple = "true" >
                                    <% ArrayList selectedCustomersList = ((TopCustomers) topCustomers).getSelectedCustomersList();
                                        pageContext.setAttribute("selectedCustomersList", selectedCustomersList); %>
                                <html:options collection="selectedCustomersList" property="value" labelProperty="label"/>
                            </nested:select>
                        </TD>
                        <TD width="20%" <%if(stageItemid.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%>>
                                <nested:text name="topCustomers" property="customersAlisName" size="30" maxlength="30" styleClass="txfield" onkeypress="return noenter( event )" />
                        </TD>
                        <TD width="20%">

                        </TD>
                    </TR>
                    </nested:iterate>
                    <TR><TD colspan="5" width="90%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></TD></TR>
                    <TR>
                        <TD align=center colspan=5>
                            <chiptrack:restrictedLink functionality='<%=Functionality.CUSTOMER_DETAILS%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:saveTopCustomers();" onmouseover="window.status='Click here to save Top Customers';return true" onmouseout="window.status='';return true">
                                <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17"  onmouseover="window.status='Click here to save Top customers';return true" onmouseout="window.status='';return true" title="Click here to save Top Customers" >
                            </chiptrack:restrictedLink>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <a href="<%=chipTracContext%>/topCustomers.do"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17" onmouseover="window.status='Click to Cancel Changes';return true" onmouseout="window.status='';return true" title="Click here to Cancel Changes"></a>
                        </TD>
                    </TR>
                </TABLE>

            </td>
        </tr>
    </table>

    </nested:nest>
     </logic:equal>
    <logic:notEqual name="showCustomers" value="true" >
        <TR><TD colspan="5" width="90%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=500></TD></TR>
     </logic:notEqual>
    <input type="hidden" name="action">
    </html:form>
    </TD>
    <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</HTML>

<%
    }
    catch(Exception e){
        e.printStackTrace();}
   
%>